package com.cq.sphtech.util;

import android.util.Log;

import com.cq.sphtech.model.FlowInfo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class DataUtils {

    public static List<List<FlowInfo.RecordsBean>> DealInfo(FlowInfo flowInfo) {
        List<FlowInfo.RecordsBean> records = flowInfo.getRecords();
        List<List<FlowInfo.RecordsBean>> flowList = new ArrayList<>();
        // 对插入数据排序
        Map<Long, List<FlowInfo.RecordsBean>> YearMap = new TreeMap<>(new Comparator<Long>() {
            @Override
            public int compare(Long o1, Long o2) {
                return o1.compareTo(o2);
            }
        });
        for (FlowInfo.RecordsBean record : records) {
            List<FlowInfo.RecordsBean> tempList = YearMap.get(Long.parseLong((record.getQuarter()).substring(0, 4)));
            // 如果取不到数据,那么直接new一个空的ArrayList
            if (tempList == null) {
                tempList = new ArrayList<>();
                tempList.add(record);
                YearMap.put(Long.parseLong((record.getQuarter()).substring(0, 4)), tempList);
            } else {
                // 某个record之前已经存放过了,则直接追加数据到原来的List里
                tempList.add(record);
            }
        }
        for (Long year : YearMap.keySet()) {
            for (int i = 0; i < YearMap.get(year).size() - 1; i++) {
                Double a = Double.valueOf(YearMap.get(year).get(i).getVolume_of_mobile_data()).doubleValue();
                Double b = Double.valueOf(YearMap.get(year).get(i + 1).getVolume_of_mobile_data()).doubleValue();
                if (a > b){
                    YearMap.get(year).get(i + 1).setFlag(true);
                }
            }
            System.out.println(YearMap.get(year).toString());
            flowList.add(YearMap.get(year));
        }
        return flowList;
    }
}
